package com.zhou.windpdfdownload;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author: zhouweixin
 * @Description:
 * @Date: Created in 23:39 2018/10/12
 * @Modified By:
 */
public class Main {
    private Pattern pattern = Pattern.compile("HYPERLINK\\(\"(.*)\",");

    public static void main(String[] args) throws IOException {
        Main main = new Main();
        Set<String> urls = main.getAllUrl();
        main.getDownloadUrl(urls);
    }

    public void test() throws IOException {
        Document document = Jsoup.connect("http://snap.windin.com/ns/bulletin.php?code=B26A1813E3EB&id=89393230&type=1").timeout(5000).get();

        Elements es = document.select("a.big");
        for (Element e : es) {
            String href = e.attr("href");
            String url = "http://snap.windin.com/ns/" + href;
            System.out.println(url);
        }
    }

    /**
     * 获得所有的地址
     */
    public Set<String> getAllUrl() throws IOException {
        Set<String> urls = new HashSet<>();

        File file = new File("公司公告.xls");
        if(file.exists() == false){
            System.out.println("公司公告.xls文件不存在");
            return urls;
        }

        InputStream is = new FileInputStream(file);
        HSSFWorkbook workbook = new HSSFWorkbook(is);
        HSSFSheet sheet = workbook.getSheetAt(0);
        HSSFRow row = sheet.getRow(0);
        int colNum= -1;
        for (int i = 0; i < row.getLastCellNum(); i++) {
            Cell cell = row.getCell(i);
            String str = cell.getStringCellValue();
            if(str.contains("公告标题")){
                colNum = i;
                break;
            }
        }

        if(colNum == -1){
            System.out.println("【公司公告.xls】文件里未找到【公告标题】列");
            return urls;
        }

        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
            row = sheet.getRow(i);
            if(row != null) {
                HSSFCell cell = row.getCell(colNum);
                if (cell != null) {
                    String cellFormula = cell.getCellFormula();
                    Matcher matcher = pattern.matcher(cellFormula);
                    if (matcher.find()) {
                        urls.add(matcher.group(1));
                    }
                }
            }
        }

        return urls;
    }

    /**
     * 获取所有的下载地址
     */
    public void getDownloadUrl(Set<String> urls){
        Set<String> downloadUrls = new HashSet<>();
        Set<String> failedUrls = new HashSet<>();

        for (String url : urls){
            try {
                Document document = Jsoup.connect(url).timeout(5000).get();

                Elements es = document.select("a.big");
                for (Element e : es) {
                    String href = e.attr("href");
                    String downloadUrl = "http://snap.windin.com/ns/" + href;
                    downloadUrls.add(downloadUrl);
                }
            }catch (Exception e){
                failedUrls.add(url);
            }
        }

        for (String downloadUrl : downloadUrls) {
            System.out.println(downloadUrl);
        }
        System.out.println(downloadUrls.size());
    }
}
